
import SwiftUI
import GRDB

struct AddressBookContactView: View {
    let row: Row
    let values: [Row]
    let valueEntries: [Row]

    var body: some View {
        List {
            Group {
                row.cell(name: "First", key: "First")
                row.cell(name: "Last", key: "Last")
                row.cell(name: "Middle", key: "Middle")
                row.cell(name: "Organization", key: "Organization")
                row.cell(name: "Birthday", key: "Birthday")
                row.cell(name: "Job Title", key: "JobTitle")
                row.cell(name: "Nickname", key: "Nickname")
                row.cell(name: "Nickname", key: "Nickname")
                row.dateCell(name: "CreationDate", key: "CreationDate")
                row.dateCell(name: "ModificationDate", key: "ModificationDate")
            }
            row.numberCell(name: "Index", key: "ROWID")
            let id: Int = row["ROWID"]
            let filteredValues: [(Int, String?)] = values.filter { $0["record_id"] == id }.map { ($0["UID"], $0["value"]) }
            let presentValues = filteredValues.compactMap { $0.1 }
            ForEach(presentValues) { value in
                Text(value)
            }
            let multiValuesIds = Set(filteredValues.map { $0.0 })
            let multivalues: [String] = valueEntries.filter { multiValuesIds.contains($0["parent_id"]) }.map { $0["value"] }
            ForEach(multivalues) { value in
                Text(value)
            }
            row.cell(name: "External Identifier", key: "ExternalIdentifier")
        }
    }
}

//struct AddressBookContactView_Previews: PreviewProvider {
//    static var previews: some View {
//        AddressBookContactView()
//    }
//}
